// pages/progress/progress.js
var session = require('../../common/session');
var config = require('../../common/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ProcessingOrderId: null,
    orderProgressId:'0',
    descript: '',
    showTextBox: false,
    hideOperat: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      ProcessingOrderId : options.ProcessingOrderId,
      orderProgressId: options.orderProgressId
    })
    this.getProcessingByOrderId();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarTitle({
      title: '进度详情'
    });
  },
  // 提交
  submit:function(){
    if(!this.data.showTextBox){
      this.showTextBox();
      return;
    }
    let _this = this;
    if(this.data.descript.trim().length > 0){
      wx.createSelectorQuery().select('.curProcess').boundingClientRect((res) => {
        let id = res.dataset.id;
        wx.request({
          url: config.service.UpdateOrderProgress,
          method: 'PUT',
          header: {
            // 'Content-Type': 'application/x-www-form-urlencoded', // 默认值
            'Authorization': 'Bearer ' + session.get('xyznzzaccessToken')
          },
          data: {
            ProcessingOrderId: _this.data.ProcessingOrderId,
            OrderProgressDetailId: id,
            Remark: _this.data.descript
          },
          success: function (res) {
            if(res.data && res.data.result && res.data.result == '200'){
                wx.showToast({
                  title: '操作成功',
                  icon: 'success',
                  duration: 2000,
                  mask: true,
                  success: function () {
                    _this.getProcessingByOrderId();
                    _this.setData({
                      showTextBox: false,
                      descript: ''
                    })
                  }
                });
            } else if(res.statusCode == '401'){
              wx.showToast({
                title: '登录失效，请重新登录',
                icon: 'none',
                duration: 2000,
                mask: true,
                success: function () {
                  session.allClear();
                  setTimeout(function(){
                    wx.reLaunch({
                      url: 'pages/syslogin/syslogin'
                    })
                  },2000);
                 
                }
              });
            }
          }
        });
        }).exec()
    } else {
      wx.showToast({
        title: '描述不能为空',
        icon: 'none',
        duration: 2000,
        mask: true
      })
    }
  },
  bindKeyInput: function (e) {
    this.setData({
      descript: e.detail.value
    })
  },
  // 获取进度
  getProcessingByOrderId:function(){
    wx.showLoading({
      title: '加载中...',
    });
    let _this = this,_url=config.service.GetOrderProgressListById;
    if(!this.data.orderProgressId || this.data.orderProgressId == '0'){
      _url = config.service.GetWeiXinOrderNode;
    }
    wx.request({
      url: _url,
      data: {
        Id: _this.data.ProcessingOrderId
      },
      method: 'GET',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded', // 默认值
        'Authorization': 'Bearer ' + session.get('xyznzzaccessToken')
      },
      success: function (res) {
        if(res.data && res.data.result && res.data.result instanceof Array){
          _this.setData({
            result:res.data.result
          });
          setTimeout(function(){

            wx.createSelectorQuery().select('.curProcess').boundingClientRect((res) => {
              if(!res) {
                _this.setData({
                  hideOperat:true
                })
              } else {
                _this.setData({
                  hideOperat:false
                })
              }
              }).exec()
          },0);

        } else if(res.statusCode == '401'){
          wx.showToast({
            title: '登录失效，请重新登录',
            icon: 'none',
            duration: 2000,
            mask: true,
            success: function () {
              session.allClear();
              setTimeout(function(){
                wx.reLaunch({
                  url: 'pages/syslogin/syslogin'
                })
              },2000);
             
            }
          });
        }
      },
      complete:function(){
        wx.hideLoading();
      }
    });
  },
  showTextBox:function(){
    this.setData({
      showTextBox: true
    })
  },
  hideTextBox:function(){
    this.setData({
      showTextBox: false
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})